package com.five.matebook.activity;

import android.content.Context;
import android.os.Handler;
import android.os.Looper;
import android.text.TextUtils;
import android.util.ArrayMap;
import android.view.View;
import android.view.inputmethod.InputMethodManager;
import android.widget.Spinner;
import android.widget.TextView;
import android.widget.Toast;

import com.five.matebook.R;
import com.five.matebook.activity.db.OneQueryData;
import com.five.matebook.activity.db.TableBase;
import com.five.matebook.utils.LogUtils;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

public class RecordForm {
    private static final String TAG = RecordForm.class.getSimpleName();
    public Map<String, View> mMaps = new ArrayMap<>();
    private List<String> mKeyList = new ArrayList<>();
    private Context mContext;
    private Handler mMainHandler = new android.os.Handler(Looper.getMainLooper());
    private TableBase mTable;
    public RecordForm(Context context, TableBase table) {
        this.mContext = context;
        this.mTable = table;
    }
    public RecordForm bind(String key, View view, String name) {
        return bind(key, view, name, false);
    }

    public RecordForm bind(String key, View view, String name, boolean nullable) {
        return bind(key, view, name, nullable, null);
    }

    public RecordForm bind(String key, View view, String name, boolean nullable, FormEleTypeTransfer transfer) {
        if (TextUtils.isEmpty(key) || view == null || TextUtils.isEmpty(name)) {
            LogUtils.e(TAG, "bind() failed! invalid args");
            return this;
        }
        view.setTag(R.integer.record_form_tag_name, name);
        view.setTag(R.integer.record_form_tag_nullable, nullable ? true : null);
        view.setTag(R.integer.record_form_ele_transformer, transfer);

        mMaps.put(key, view);
        mKeyList.remove(key);
        mKeyList.add(key);
        return this;
    }

    private String getComponentString(View v) {
        if (v instanceof Spinner) {
            Object obj = ((Spinner)v).getSelectedItem();
            return obj != null ? obj.toString() : "";
        } else if (v instanceof TextView) {
            return ((TextView) v).getText().toString();
        }
        return "";
    }

    /**
     * @return 成功: 记录id  失败: null
     */
    public OneQueryData submit() {
        return submit(null);
    }

    public OneQueryData submit(Long id) {
        Map<String, Object> data = new HashMap<>();
        for(String key : mKeyList) {
            View v = mMaps.get(key);
            String value = getComponentString(v);
            boolean nullable = v.getTag(R.integer.record_form_tag_nullable) != null;

            if (TextUtils.isEmpty(value) && !nullable) {
                String name = (String) v.getTag(R.integer.record_form_tag_name);
                Toast.makeText(mContext, name + " 没有填写", Toast.LENGTH_SHORT).show();
                v.requestFocus();
                InputMethodManager inputMethodManager = (InputMethodManager) mContext.getSystemService(Context.INPUT_METHOD_SERVICE);
                inputMethodManager.toggleSoftInput(InputMethodManager.SHOW_FORCED, 0);
                return null;
            }

            FormEleTypeTransfer transfer = (FormEleTypeTransfer) v.getTag(R.integer.record_form_ele_transformer);
            data.put(key, transfer != null ? transfer.getValue(value) : value);
        }

        OneQueryData ret = null;
        if (id == null) {
            // 新增
            ret = mTable.add(data);
        } else {
            // 更新
            ret = mTable.update(id, data);
        }
        Toast.makeText(mContext, "提交成功", Toast.LENGTH_SHORT).show();

        return ret;
    }

    public interface FormEleTypeTransfer {
        Object getValue(String v);
    }
}
